package com.leetcode.题库;

import com.leetcode.common.model.TreeNode;

/**
 * @author 覃国强
 * @date 2022-05-02 14:48
 */
public class 题库_0235_二叉搜索树的最近公共祖先 {


  static
  class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
      if (root == null) {
        return null;
      }
      if (p.val < root.val && q.val < root.val) {
        return lowestCommonAncestor(root.left, p, q);
      }
      if (p.val > root.val && q.val > root.val) {
        return lowestCommonAncestor(root.right, p, q);
      }
      return root;
    }
  }

}
